let arr = [{
    "id": 1,
    "name": "部门1",
    "pid": 0,
    "children": [{
            "id": 2,
            "name": "部门2",
            "pid": 1,
            "children": []
        },
        {
            "id": 3,
            "name": "部门3",
            "pid": 1,
            "children": [{
                "id": 4,
                "name": "部门4",
                "pid": 3,
                "children": [{
                    "id": 5,
                    "name": "部门5",
                    "pid": 4,
                    "children": []
                }]
            }]
        }
    ]
}];

function treeToArray(arr) {
    let flatArr = [];
    for (let i = 0; i < arr.length; i++) {
        flatArr.push(arr[i]);
       
        if ( arr[i].children.length > 0) {
           
            treeToArray(arr[i].children)
        }
    }
    console.log('flatArr',flatArr)
}
treeToArray(arr)
